package com.shopping.dev.service.impl;

import com.shopping.dev.bo.AddressBO;
import com.shopping.dev.common.enums.YesOrNoEnum;
import com.shopping.dev.common.util.IdUtil;
import com.shopping.dev.mapper.UserAddressMapper;
import com.shopping.dev.pojo.UserAddress;
import com.shopping.dev.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> queryAll(String userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        return userAddressMapper.select(userAddress);
    }

    @Transactional
    @Override
    public void addNewUserAddress(AddressBO addressBO) {
        List<UserAddress> userAddresses = this.queryAll(addressBO.getUserId());
        UserAddress u = new UserAddress();
        if (userAddresses == null || userAddresses.isEmpty()) {
            u.setIsDefault(1);
        }
        BeanUtils.copyProperties(addressBO, u);
        u.setId(IdUtil.getUUID());
        u.setCreatedTime(new Date());
        u.setUpdatedTime(new Date());
        userAddressMapper.insert(u);
    }

    @Transactional
    @Override
    public void updateUserAddress(AddressBO addressBO) {
        UserAddress u = new UserAddress();
        BeanUtils.copyProperties(addressBO, u);
        u.setUpdatedTime(new Date());
        u.setId(addressBO.getAddressId());
        userAddressMapper.updateByPrimaryKeySelective(u);
    }

    @Transactional
    @Override
    public void deleteUserAddress(String userId, String addressId) {
        UserAddress u = new UserAddress();
        u.setId(addressId);
        u.setUserId(userId);
        userAddressMapper.delete(u);
    }

    @Transactional
    @Override
    public void upateUserAddressToDefault(String userId, String addressId) {
        Example example = new Example(UserAddress.class);
        example.createCriteria()
                .andEqualTo("isDefault", YesOrNoEnum.YES.getCode())
                .andEqualTo("userId", userId);
        List<UserAddress> userAddresses = userAddressMapper.selectByExample(example);
        if (userAddresses != null && !userAddresses.isEmpty()) {
            for (UserAddress a : userAddresses) {
                a.setIsDefault(YesOrNoEnum.NO.getCode());
                a.setUpdatedTime(new Date());
                userAddressMapper.updateByPrimaryKeySelective(a);
            }
        }
        UserAddress u = new UserAddress();
        u.setId(addressId);
        u.setUserId(userId);
        u.setIsDefault(YesOrNoEnum.YES.getCode());
        u.setUpdatedTime(new Date());
        userAddressMapper.updateByPrimaryKeySelective(u);
    }

    @Override
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress u = new UserAddress();
        u.setId(addressId);
        u.setUserId(userId);
        return userAddressMapper.selectOne(u);
    }
}
